package com.hzncc.scaffold.controller.Front.Ask;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzncc.scaffold.core.ResultT;
import com.hzncc.scaffold.entity.Bo.AskDetailBo;
import com.hzncc.scaffold.entity.Bo.CourseAskBo;
import com.hzncc.scaffold.entity.Other.Ask;
import com.hzncc.scaffold.entity.Vo.*;
import com.hzncc.scaffold.mapper.AskMapper;
import com.hzncc.scaffold.service.AskService;
import com.hzncc.scaffold.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.sql.Struct;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: DrunkBear
 * @Date: 2022-06-14 15:46
 * @Description: null
 */
@RequestMapping("/course/ask")
@Api(tags = "前台接口-问题")
@RestController
public class AskController {

    @Autowired
    AskMapper askMapper;

    @Autowired
    AskService askService;

    @Autowired
    UserService userService;


    @GetMapping("/list")
    @ApiOperation("问题列表")
    public ResultT list(Integer pageNum,Integer pageSize,String keyword,Integer orderNew,Integer needReply,Integer courseId){


        QueryWrapper<CourseAskBo> qw = new QueryWrapper<>();
        qw.eq("ask.status",1);
        qw.eq("ask.courseId",courseId);
        if(StrUtil.isNotEmpty(keyword)){
            qw.like("ask.title",keyword);
        }

        if(orderNew == null){
            qw.orderByDesc("ask.scannedNum");
            qw.orderByDesc("ask.replyNum");
        }else if(orderNew == 1){
            qw.orderByDesc("ask.pubTime");
        }

        if(needReply!=null && needReply == 1){
            qw.eq("ask.lastReplyTime","0000-00-00 00:00:00");
            qw.orderByDesc("ask.scannedNum");
            qw.orderByDesc("ask.replyNum");
        }

        if (pageNum != null && pageSize != null && pageNum > 0 && pageSize > 0) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<CourseAskBo> courseAskBos = askMapper.courseAsk(qw);
        ArrayList<CourseAskVo> courseAskVos = new ArrayList<>();


        for (CourseAskBo courseAskBo : courseAskBos) {
            CourseAskVo courseAskVo = BeanUtil.copyProperties(courseAskBo, CourseAskVo.class,"tag");

            AskCreatorVo askCreatorVo = new AskCreatorVo();
            askCreatorVo.setId(courseAskBo.getCreatorId());
            askCreatorVo.setName(courseAskBo.getCreatorName());
            askCreatorVo.setIcon(courseAskBo.getCreatorIcon());

            courseAskVo.setCreator(askCreatorVo);

            if(StrUtil.isNotEmpty(courseAskBo.getTag())){
                List<String> tagList = StrUtil.split(courseAskBo.getTag(), "&&");
                courseAskVo.setTag(tagList);
            }
            courseAskVos.add(courseAskVo);
        }

        PageInfo<CourseAskVo> info = new PageInfo<>();
        info.setList(courseAskVos);
        info.setTotal(new PageInfo<>(courseAskBos).getTotal());

        return ResultT.success(info);
    }

    @PostMapping("/commit")
    @ApiOperation("提交问题")
    public ResultT commit(@RequestBody CommitAskVo commitAskVo){
        if(!StpUtil.isLogin()){
            return ResultT.fail("数据错误");
        }

        String courseId = commitAskVo.getCourseId();
        String content = commitAskVo.getContent();
        List<String> tagList = commitAskVo.getTag();
        String title = commitAskVo.getTitle();
        StringBuilder tags = new StringBuilder();


        Ask ask = new Ask();
        ask.setTitle(title);
        ask.setContent(content);
        ask.setCourseId(Integer.parseInt(courseId));
        ask.setCreateTime(DateUtil.now());
        ask.setUserId(StpUtil.getLoginIdAsInt());
        ask.setLastReplyTime("0000-00-00 00:00:00");


        for (String tag : tagList) {
            tags.append(tag).append("&&");
        }
        if(tags.length() > 0){
            tags = new StringBuilder(tags.substring(0, tags.length() - 2));
        }
        ask.setTag(tags.toString());

        boolean save = askService.save(ask);
        if(save){
            return ResultT.success("操作成功");
        }else {
            return ResultT.fail("操作失败");
        }
    }

    @GetMapping("/detail")
    @ApiOperation("问题详情")
    public ResultT detail(Integer askId){

        QueryWrapper<AskDetailBo> qw = new QueryWrapper<>();
        qw.eq("ask.id",askId);
        AskDetailBo askDetailBo = askMapper.askDetail(qw);
        AskDetailVo askDetailVo = BeanUtil.copyProperties(askDetailBo, AskDetailVo.class,"tag");

        if(StrUtil.isNotEmpty(askDetailBo.getTag())){
            List<String> tagList = StrUtil.split(askDetailBo.getTag(), "&&");
            askDetailVo.setTag(tagList);
        }

        AskCreatorVo askCreatorVo = new AskCreatorVo();
        askCreatorVo.setId(askDetailBo.getCreatorId());
        askCreatorVo.setName(askDetailBo.getCreatorName());
        askCreatorVo.setIcon(askDetailBo.getCreatorIcon());

        askDetailVo.setCreator(askCreatorVo);

        QueryWrapper<AskReplyVo> qww = new QueryWrapper<>();
        qww.eq("reply.askId",askDetailBo.getId());
        qww.eq("reply.status",1);

        List<AskReplyVo> askReplyVos = askMapper.replyList(qww);
        ArrayList<Replies> repliesList = new ArrayList<>();

        for (AskReplyVo askReplyVo : askReplyVos) {
            Replies replies = BeanUtil.toBean(askReplyVo, Replies.class);

            AskCreatorVo creatorVo = new AskCreatorVo();
            if(userService.getById(askReplyVo.getUserId())!=null){
                creatorVo = BeanUtil.toBean(userService.getById(askReplyVo.getUserId()), AskCreatorVo.class);
            }
            replies.setCreator(creatorVo);
            repliesList.add(replies);
        }

        askDetailVo.setReplies(repliesList);

        //浏览次数加一
        Ask ask = askService.getById(askId);
        Integer scannedNum = ask.getScannedNum();
        ask.setScannedNum(scannedNum + 1);
        askService.updateById(ask);

        return ResultT.success(askDetailVo);
    }
}
